//
// Description: 49. 字母异位词分组
// Created by Loading on 2022/4/3.
//

#include <bits/stdc++.h>

using namespace std;

vector<vector<string>> groupAnagrams(vector<string> &strs) {
    vector<vector<string>> res;
    unordered_map<string, vector<string>> m;

    for (auto &s : strs) {
        string key = s;
        sort(key.begin(), key.end());
        m[key].emplace_back(s);
    }

    for (auto[_, value] : m) {
        res.emplace_back(value);
    }

    return res;
}

int main() {
    vector<string> strs{"eat", "tea", "tan", "ate", "nat", "bat"};
    auto res = groupAnagrams(strs);
    for (auto v : res) {
        for (auto s : v) {
            cout << s << ' ';
        }
        cout << endl;
    }

    return 0;
}